//
//  LSYGuideMainView.m
//  TTClub
//
//  Created by tangkaifu on 15/12/31.
//  Copyright © 2015年 TTClub. All rights reserved.
//

#import "LSYGuideMainView.h"
#import <Masonry.h>
static NSString *const LSYGuideMianViewCollecionViewCellWithReuseId = @"LSYGuideMianViewCollecionViewCell";


@interface LSYGuideMianViewCollecionViewCell : UICollectionViewCell

@property (nonatomic, strong) UIImage     *image;
@property (nonatomic, strong) UIImageView * imageView;
@property (nonatomic, strong) UIButton     *tasteButton;

@end

@implementation LSYGuideMianViewCollecionViewCell


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if(self){
        
        self.backgroundColor = [UIColor clearColor];

        _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        
        if(LYScreenHeight > 320){
            
            _imageView.contentMode = UIViewContentModeScaleAspectFill;
        }else{
            _imageView.contentMode = UIViewContentModeScaleAspectFill;

        }
        
        [self addSubview:_imageView];
        [self addSubview:self.tasteButton];
        
        WS(weakSelf)
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.edges.mas_equalTo(weakSelf);
        }];
        
    }
    
    return self;
}

- (void)setImage:(UIImage *)image{
    
    _image = image;
    self.imageView.image = image;
}


- (UIButton *)tasteButton{
    
    if(_tasteButton == nil){
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(ceilf((self.My_Width - 150) / 2), self.My_Height - 150, 150, 100)];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [button setTitle:@"开始体验" forState:UIControlStateNormal];

        _tasteButton = button;
        _tasteButton.backgroundColor = [UIColor clearColor];
        
    }
    
    return _tasteButton;
}


@end


static LSYGuideMainView * _instance;

@interface LSYGuideMainView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray <UIImage *>* imageArray;
@property (nonatomic, strong) UIButton        *loginButton;
@property (nonatomic, strong) UIPageControl    *pageControl;

@property (nonatomic, assign) CGFloat          buttonHeight;
@property (nonatomic, assign) CGFloat          pageControlHeight;
@property (nonatomic, assign) CGFloat          pageControlWidth;
@property (nonatomic, assign,getter=isExecuteDismissAnation) BOOL          executeDismissAnamtion;

@property (nonatomic, copy) LSYTTClubButtonPressedBlock buttonPressedBlock;

@end

@implementation LSYGuideMainView

+ (instancetype)showGuideMainViewWithButtonPressedBlock:(LSYTTClubButtonPressedBlock)buttonPressedBlock
{

    NSString *versionStr = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
    NSString *key = [NSString stringWithFormat:@"KEY_OF_LSYGuideMainViewShow_FROM_%@", versionStr];
    BOOL haveShow = [[NSUserDefaults standardUserDefaults] boolForKey:key];
    
    LSYGuideMainView *guideMainView = nil;
    if (!haveShow) {
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
        
        guideMainView = [[LSYGuideMainView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth,LYScreenHeight)];
        [guideMainView setAlpha:0.0f];
        guideMainView.buttonPressedBlock = buttonPressedBlock;
        [[UIApplication sharedApplication].keyWindow addSubview:guideMainView];
        
        [UIView animateWithDuration:0.33 animations:^{
            [guideMainView setAlpha:1.0f];
        }];
        
    }else {
        if (buttonPressedBlock) {
            buttonPressedBlock(0, nil);
        }
    }

    return guideMainView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self p_setDefaultValue];
        
        self.backgroundColor = [UIColor clearColor];

        [self addSubview:self.collectionView];
        [self addSubview:self.pageControl];
        self.pageControl.hidden = YES;
        NSMutableArray *array = [NSMutableArray array];
        for(int i = 1; i < 5; i ++){
            
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"引导页%@",@(i)]];
            [array addObject:image];
        }
        [self setImageArray:array];
        [self p_makeConstraints];
    }
    return self;
}

- (void)dealloc
{
    NSLog(@"LSYGuideMainView dealloc");
}

#pragma mark 

- (void)setImageArray:(NSArray<UIImage *> *)imageArray{
    
    _imageArray = imageArray;
    
    self.pageControl.numberOfPages = imageArray.count;
    [self.collectionView reloadData];
}

#pragma mark - UI

- (void)p_setDefaultValue{
    self.pageControlHeight = 30;
    self.pageControlWidth = 100;
    self.buttonHeight = 44;
}
- (void)p_makeConstraints{
    
    WS(weakSelf)
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(weakSelf);
    }];

    
    //[self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //make.left.mas_equalTo(weakSelf.mas_centerX).offset( 20);
        //make.right.mas_equalTo(weakSelf).offset( - g_fitFloat(@[@32, @40, @49]));
        //make.top.bottom.mas_equalTo(weakSelf.registerButton);
    //}];

//    [self.registerButton mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(weakSelf.mas_centerX);
//        make.bottom.mas_equalTo(weakSelf.mas_bottom).offset(-g_fitFloat(@[@120,@120,@140]));
//        make.width.mas_equalTo(150);
//        make.height.mas_equalTo(weakSelf.buttonHeight);
//        
//    }];
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(weakSelf.pageControlHeight);
        make.width.mas_equalTo(weakSelf.pageControlWidth);
        make.centerX.mas_equalTo(weakSelf);
        make.bottom.mas_equalTo(weakSelf.mas_bottom).offset(-20);
    }];

}


- (UIPageControl *)pageControl{
    
    if(_pageControl == nil){
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
        _pageControl.pageIndicatorTintColor = LYColorWithHex(0xeaeaea);
        _pageControl.currentPageIndicatorTintColor = LYColorWithHex(0xfc4578);
        _pageControl.numberOfPages = self.imageArray.count;
        _pageControl.userInteractionEnabled = NO;
    }
    
    return _pageControl;
}
- (UICollectionView *)collectionView{
    
    if(_collectionView == nil){

        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        layout.itemSize = [UIScreen mainScreen].bounds.size;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [_collectionView registerClass:[LSYGuideMianViewCollecionViewCell class] forCellWithReuseIdentifier:LSYGuideMianViewCollecionViewCellWithReuseId];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
        
    return _collectionView;
}

#pragma mark - buttonClick

- (void)loignButtonDidClick:(UIButton *)button{
    
    if (self.buttonPressedBlock) {
        self.buttonPressedBlock(button.tag, button);
    }
    
    UIView *toView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
    
    [UIView transitionFromView:self toView:toView duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    LSYGuideMianViewCollecionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:LSYGuideMianViewCollecionViewCellWithReuseId forIndexPath:indexPath];
    
    [cell setImage:self.imageArray[indexPath.item]];
    
    [cell.tasteButton addTarget:self action:@selector(loignButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    
    if (indexPath.item == self.imageArray.count - 1){
        cell.tasteButton.hidden = NO;

    }else{
        cell.tasteButton.hidden  = YES;
    }
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 设置指示器当前页
    _pageControl.currentPage = scrollView.contentOffset.x / self.My_Width;
    
    
    if (scrollView.contentOffset.x - scrollView.My_Width * 3 > 20){
        [self setLoginButton:nil];
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x < 0.0) {
        CGPoint contentOffset = scrollView.contentOffset;
        contentOffset.x = 0.0;
        scrollView.contentOffset = contentOffset;
        return;
    }
    
    if (scrollView.contentOffset.x - (scrollView.contentSize.width - scrollView.My_Width) > 100){
        // 消失
        if (self.isExecuteDismissAnation == NO){
            [self loignButtonDidClick:self.loginButton];
            self.executeDismissAnamtion = YES;
        }
    }
    
    
    // 控制按钮
    
//    self.registerButton.alpha = (scrollView.contentOffset.x - 2 * scrollView.width) / scrollView.width;
}


#pragma mark - Getter

- (UIButton *)loginButton{
    
    if(_loginButton == nil){
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
        [button setTitle:@"登录" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [button addTarget:self  action:@selector(loignButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:1];
        _loginButton = button;
    }
    return _loginButton;
}



@end


